﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.Sockets;
using InfoPick.Lib.StringResolve;
using System.Threading;
using InfoPick.Lib.Entity;
using InfoPick.Lib.Telnet;
using InfoPick.Lib.DataProvider;
using InfoPick.Lib.Tools;
using System.Data;
using InfoPick.Lib.Service;

namespace InfoPick.Lib.DataPicker
{

    public class DataPicker
    {
        private event Action<string> onNotice;

        private event Action<string,string> onCmdSuccess;

        private DataService service;

        public DataPicker(Action<string> OnNotice = null,Action<string,string> OnCmdSuccess = null)
        {
            this.onNotice = OnNotice;
            this.onCmdSuccess = OnCmdSuccess;
        }

        public Data Start(Server mgw, Server msc, string excelFullPath, string mgwPath = null, string mscPath = null)
        {
            var  provider = DataProviderFactory.GetDataProvider(mgw, msc, mgwPath, mscPath);
            provider.OnNotice += onNotice;
            provider.OnCmdSuccess += onCmdSuccess;

            service=new DataService(provider);

            Data data = null;

            //开始获取数据
            if (service.Begin())
            {
                Notice("开始分析.......\r");
                //执行分析算法
                data = execute();

                //获取数据完成
                service.Complete();

                Notice("分析完成，开始生成数据...\r");

                //从原始excel中读取信息
                loadBaseData(excelFullPath,data);

                Notice("生成数据完成\r");
            }

            return data;
        }

        protected void Notice(string msg)
        {
            if (onNotice != null)
                onNotice(msg);
        }

        /// <summary>
        /// 流程
        /// </summary>
        private Data execute()
        {
            Data data = new Data();
            #region gmddf 和 gmodf
            //得到TDMIU信息
            data.TDMIUList = service.GetTdmiuList();
            //根据编码规则初始化GMDDF和GMODF
            data.GMDDFListProgram = InitGMDDFList(data.TDMIUList);
            data.GMODFListProgram = InitGMODFList(data.TDMIUList);

            {
                //查找N7tkc
                data.N7TKCList = service.GetN7TKCList();
                //填入N7tkc信息，方向、SC、EC、StartCic、StartTid
                fillN7tkc(data.GMDDFListProgram, data.N7TKCList);
                //填入系统编号，即排序
                fillSystem(data.GMDDFListProgram);
            }
            {
                //查找Aietkc
                data.AIETKCList = service.GetAIETKCList();
                //查找Aietg
                data.AIETGList = service.GetAIETGList();
                //填充方向(中继群名称)到AIETKC
                fillDirection(data.AIETKCList, data.AIETGList);
                //填入Aietkc信息，方向、SC、EC、StartCic、StartTid
                fillAietkc(data.GMODFListProgram, data.AIETKCList);
                //插值
                Interpolation(data.GMODFListProgram);
                //填入系统编号，即排序
                fillSystem(data.GMODFListProgram);
            }


            //除了TKC，另外一部分的板子的信息在MTP2上，查询方向，SPF，扣板号，链路时隙
            data.GMN7LNKList = service.GetN7LNKList();
            fillMTP2(data.GMDDFListProgram, data.GMODFListProgram, data.GMN7LNKList);
            
            #endregion

            #region gsddf
            data.GSDDFListProgram = InitGSDDFList();
            data.GSN7LNKAllList = service.GetN7LNKALLList();
            fillN7(data.GSDDFListProgram, data.GSN7LNKAllList);

            data.CLKSRCList = service.GetCLKSRCList();
            //填写备注
            fillRemark(data.GSDDFListProgram, data.CLKSRCList);
            #endregion

            return data;
        }


        private void loadBaseData(string excelFullPath,Data data)
        {
            Notice("读取原始excel！\r");
            var ds = ExcelTools.ReadBaseData(excelFullPath);
            {
                DataTable table = ds.Tables[0];

                if (table.Rows.Count == 0)
                {
                    Notice(string.Format("sheet:‘{0}’格式错误或者没有数据！\r", table.TableName));
                }
                else
                {
                    Notice(string.Format("分析{0}！\r", table.TableName));
                    var mapping = ExcelMapping.GetGMDDFMapping();
                    var q = from row in table.AsEnumerable()
                            join ddf in data.GMDDFListProgram.Where(c => c.Frame != null && c.BoardNum != null && c.SLotNum != null && c.Port != null)
                            on new { Frame = row[mapping["Frame"]].ToString(), Board = row[mapping["Board"]].ToString(), Slot = row[mapping["Slot"]].ToString(), Port = row[mapping["Port"]].ToString() } equals
                                new { Frame = ddf.Frame.ToString(), Board = ddf.BoardNum.ToString(), Slot = ddf.SLotNum.ToString(), Port = ddf.Port.ToString() }
                                into t
                            from a in t.DefaultIfEmpty()
                            select new
                            {
                                Row = row,
                                DDF = a
                            };

                    List<GMDDF> list = new List<GMDDF>();
                    foreach (var item in q)
                    {
                        GMDDF obj = new GMDDF();
                        obj.Name = item.Row[mapping["Name"]].ToString();
                        obj.GroupNum = item.Row[mapping["Group"]].ToString();
                        int tmp;
                        if (int.TryParse(item.Row[mapping["Frame"]].ToString(), out tmp))
                            obj.Frame = tmp;
                        if (int.TryParse(item.Row[mapping["Board"]].ToString(), out tmp))
                            obj.BoardNum = tmp;
                        if (int.TryParse(item.Row[mapping["Slot"]].ToString(), out tmp))
                            obj.SLotNum = tmp;
                        if (int.TryParse(item.Row[mapping["Port"]].ToString(), out tmp))
                            obj.Port = tmp;
                        
                        if (item.DDF != null)
                        {
                            obj.GlobalNum = item.DDF.GlobalNum;
                            obj.GlobalTid = item.DDF.GlobalTid;
                            obj.No7Link = item.DDF.No7Link;
                            obj.System = item.DDF.System;
                            obj.Direction = item.DDF.Direction;
                            obj.Remark = item.DDF.Remark;
                            obj.BSGNum = item.DDF.BSGNum;
                            obj.SPFNum = item.DDF.SPFNum;
                            obj.KNum = item.DDF.KNum;
                            obj.CCUNum = item.DDF.CCUNum;
                            obj.SCNum = item.DDF.SCNum;
                            obj.ECNum = item.DDF.ECNum;
                            obj.StartCic = item.DDF.StartCic;
                            obj.StartTid = item.DDF.StartTid;
                        }
                        list.Add(obj);
                    }
                    list = list.OrderBy(c => c.Frame).ThenBy(c => c.BoardNum).ThenBy(c => c.SLotNum).ThenBy(c => c.Port).ToList();
                    fillIdx(list);
                    data.GMDDFListExcel = list;
                }
            }
            {
                DataTable table = ds.Tables[1];
                if (table.Rows.Count == 0)
                {
                    Notice(string.Format("sheet:‘{0}’格式错误或者没有数据！\r", table.TableName));
                }
                else
                {
                    Notice(string.Format("分析{0}！\r", table.TableName));
                    var mapping = ExcelMapping.GetGSDDFMapping();
                    
                    var q = from row in table.AsEnumerable()
                            join ddf in data.GSDDFListProgram.Where(c => c.Frame != null && c.SLotNum != null && c.Port != null)
                            on new { Frame = row[mapping["Frame"]].ToString(), Slot = row[mapping["Slot"]].ToString(), Port = row[mapping["Port"]].ToString() } equals
                                new { Frame = ddf.Frame.ToString(), Slot = ddf.SLotNum.ToString(), Port = ddf.Port.ToString() }
                                into t
                            from a in t.DefaultIfEmpty()
                            select new
                            {
                                Row = row,
                                ODF = a
                            };
                    List<GSDDF> list = new List<GSDDF>();
                    foreach (var item in q)
                    {
                        GSDDF obj = new GSDDF();
                        obj.Name = item.Row[mapping["Name"]].ToString();
                        obj.GroupNum = item.Row[mapping["Group"]].ToString();
                        int tmp;
                        if (int.TryParse(item.Row[mapping["Frame"]].ToString(), out tmp))
                            obj.Frame = tmp;
                        if (int.TryParse(item.Row[mapping["Slot"]].ToString(), out tmp))
                            obj.SLotNum = tmp;
                        if (int.TryParse(item.Row[mapping["Port"]].ToString(), out tmp))
                            obj.Port = tmp;

                        if (item.ODF != null)
                        {
                            obj.LinkType = item.ODF.LinkType;
                            obj.No7Link = item.ODF.No7Link;
                            obj.Direction = item.ODF.Direction;
                            obj.System = item.ODF.System;
                            obj.Remark = item.ODF.Remark;
                            obj.CSUNum = item.ODF.CSUNum;
                        }
                        list.Add(obj);
                    }
                    list = list.OrderBy(c => c.Frame).ThenBy(c => c.SLotNum).ThenBy(c => c.Port).ToList();
                    fillIdx(list);

                    data.GSDDFListExcel = list;
                }
            }
            {
                DataTable table = ds.Tables[2];
                if (table.Rows.Count == 0)
                {
                    Notice(string.Format("sheet:‘{0}’格式错误或者没有数据！\r", table.TableName));
                }
                else
                {
                    Notice(string.Format("分析{0}！\r", table.TableName));
                    var mapping = ExcelMapping.GetGMODFMapping();
                    
                    var q = from row in table.AsEnumerable()
                            join ddf in data.GMODFListProgram.Where(c => c.Frame != null && c.BoardNum != null && c.SLotNum != null && c.Port != null)
                            on new { Frame = row[mapping["Frame"]].ToString(), Board = row[mapping["Board"]].ToString(), Slot = StringTools.ToSBC(row[mapping["Slot"]].ToString()), Port = row[mapping["Port"]].ToString() } equals
                                new { Frame = ddf.Frame.ToString(), Board = ddf.BoardNum.ToString(), Slot = StringTools.ToSBC(ddf.SLotNumStr), Port = ddf.PortStr }
                                into t
                            from a in t.DefaultIfEmpty()
                            select new
                            {
                                Row = row,
                                DDF = a
                            };

                    List<GMODF> list = new List<GMODF>();
                    foreach (var item in q)
                    {
                        GMODF obj = new GMODF();
                        obj.Name = item.Row[mapping["Name"]].ToString();
                        obj.ODFPosition = item.Row[mapping["Position"]].ToString();
                        int tmp;
                        if (int.TryParse(item.Row[mapping["Frame"]].ToString(), out tmp))
                            obj.Frame = tmp;
                        if (int.TryParse(item.Row[mapping["Board"]].ToString(), out tmp))
                            obj.BoardNum = tmp;
                        obj.SLotNumStr = item.Row[mapping["Slot"]].ToString();
                        //if (int.TryParse(item.Row[mapping["Port"]].ToString(), out tmp))
                        //    obj.Port = tmp;
                        obj.PortStr = item.Row[mapping["Port"]].ToString();

                        if (item.DDF != null)
                        {
                            obj.SLotNum = item.DDF.SLotNum;
                            obj.LightNum = item.DDF.LightNum;
                            obj.IsPrimary = item.DDF.IsPrimary;
                            obj.GlobalNum = item.DDF.GlobalNum;
                            obj.GlobalTid = item.DDF.GlobalTid;
                            obj.No7Link = item.DDF.No7Link;
                            obj.System = item.DDF.System;
                            obj.Direction = item.DDF.Direction;
                            obj.Remark = item.DDF.Remark;
                            obj.SPFNum = item.DDF.SPFNum;
                            obj.KNum = item.DDF.KNum;
                            obj.CCUNum = item.DDF.CCUNum;
                            obj.SCNum = item.DDF.SCNum;
                            obj.ECNum = item.DDF.ECNum;
                            obj.StartCic = item.DDF.StartCic;
                            obj.StartTid = item.DDF.StartTid;
                        }
                        list.Add(obj);
                    }
                    list = list.OrderBy(c => c.Frame).ThenBy(c => c.BoardNum).ThenBy(c => c.SLotNum).ThenBy(c => c.LightNum).ThenBy(c => c.Port).ToList();
                    fillIdx(list);

                    data.GMODFListExcel = list;
                }
            }
        }

        /// <summary>
        /// 填入“系统”编号
        /// 可根据相同”方向“，按照”START_CIC“列的大小从1开始顺序编号。
        /// </summary>
        /// <param name="ddfList"></param>
        private void fillSystem(List<GMDDF> ddfList)
        {
            var group = ddfList.Where(c => !string.IsNullOrEmpty(c.Direction)).GroupBy(c => c.Direction);
            foreach (var item in group)
            {
                int idx = 1;
                var ary = item.Where(c => c.StartCic != null).OrderBy(c => c.StartCic).ToArray();
                for (int i = 0; i < ary.Length; i++)
                {
                    ary[i].System = idx++;
                }
            }
        }
        
        /// <summary>
        /// 填入“系统”编号
        /// 可根据相同”方向“，按照”START_CIC“列的大小从1开始顺序编号。
        /// </summary>
        /// <param name="odfList"></param>
        private void fillSystem(List<GMODF> odfList)
        {
            var group = odfList.Where(c => !string.IsNullOrEmpty(c.Direction))
                .GroupBy(c => c.Direction);
            foreach (var item in group)
            {
                int idx = 1;
                var ary = item.Where(c => c.StartCic != null).OrderBy(c => c.StartCic).ToArray();
                for (int i = 0; i < ary.Length; i++)
                {
                    ary[i].System = idx++;
                }
            }
        }

        /// <summary>
        /// 填入“序号”
        /// </summary>
        /// <param name="ddfList"></param>
        private void fillIdx(List<GMDDF> ddfList)
        {
            
            int idx = 1;
            foreach (var item in ddfList)
            {
                item.Id = idx++;
            }
        }

        /// <summary>
        /// 填入“序号”
        /// </summary>
        /// <param name="ddfList"></param>
        private void fillIdx(List<GSDDF> ddfList)
        {
            
            int idx = 1;
            foreach (var item in ddfList)
            {
                item.Id = idx++;
            }
        }


        /// <summary>
        /// 填入“序号”
        /// </summary>
        /// <param name="odfList"></param>
        private void fillIdx(List<GMODF> odfList)
        {
            
            int idx = 1;
            foreach (var item in odfList)
            {
                item.Id = idx++;
            }
        }

        /// <summary>
        /// 将AIETKC命令的返回结果填入页表
        /// </summary>
        /// <param name="ddfList"></param>
        /// <param name="n7tkcList"></param>
        private void fillN7tkc(List<GMDDF> ddfList, List<N7TKC> n7tkcList)
        {
            var q = from ddf in ddfList
                    join a in n7tkcList
                    on ddf.GlobalTid equals a.StartTid
                    into t
                    from a in t.DefaultIfEmpty()
                    orderby ddf.Id ascending
                    select new
                    {
                        DDF = ddf,
                        N7TKC = a
                    };

            foreach (var c in q)
            {
                if (c.N7TKC != null)
                {
                    c.DDF.Direction = c.N7TKC.GroupName;
                    c.DDF.CCUNum = c.N7TKC.ModuleNum;
                    c.DDF.SCNum = c.N7TKC.StartCic;
                    c.DDF.ECNum = c.N7TKC.EndCic;
                    c.DDF.StartCic = c.N7TKC.StartCic;
                    c.DDF.StartTid = c.N7TKC.StartTid;
                }
            }
        }
        
        /// <summary>
        /// 将AIETKC命令的返回结果填入页表
        /// </summary>
        /// <param name="odfList"></param>
        /// <param name="aietkcList"></param>
        private void fillAietkc(List<GMODF> odfList, List<AIETKC> aietkcList)
        {
            var q = from odf in odfList
                    join a in aietkcList
                    on odf.GlobalTid equals a.StartTid
                    into t
                    from a in t.DefaultIfEmpty()
                    orderby odf.Id ascending
                    select new
                    {
                        ODF = odf,
                        AIETKC = a
                    };

            foreach (var c in q)
            {
                if (c.AIETKC != null)
                {
                    c.ODF.Direction = c.AIETKC.GroupName;
                    //改动后的aietkc没有ccu了
                    //c.ODF.CCUNum = c.AIETKC.ModuleNum;
                    c.ODF.SCNum = c.AIETKC.StartCic;

                    //c.ODF.ECNum = c.AIETKC.EndCic;
                    c.ODF.ECNum = c.ODF.SCNum + 31;
                   
                    //对于备用的单板无需填写tid信息和START_CIC。
                    //因为以下属性要用于插值，所以这里也填写
                    //在最后将“备用”单板的以下属性置空
                    //if (c.ODF.IsPrimary != false)
                    {
                        c.ODF.StartCic = c.AIETKC.StartCic;
                        c.ODF.StartTid = c.AIETKC.StartTid;
                    }
                }
            }

           
           
        }

        //插值
        private void Interpolation(List<GMODF> odfList)
        {
            var grp = odfList.GroupBy(c => new { c.Frame, c.BoardNum, c.SLotNum, c.LightNum });
            foreach (var g in grp)
            {
                //这里用“方向”标识，因为其他属性在“备用”状态不会被填写
                var value = g.Where(c => c.Direction != null).FirstOrDefault();
                if (value != null)
                {
                    foreach (var item in g.Where(c => c.Direction == null))
                    {
                        item.Direction = value.Direction;
                        //缺省端口不用填写
                        if (!item.IsDefault)
                        {
                            item.StartTid = value.StartTid + (item.Port - value.Port) * 32;
                            item.StartCic = value.StartCic + (item.Port - value.Port) * 32;
                            
                            item.SCNum = value.StartCic + (item.Port - value.Port) * 32;
                            item.ECNum = item.SCNum + 31;
                        }

                    }
                }
            }

            //将“备用”单板的 startTid 和 startCic 置空
            odfList.Where(c => c.IsPrimary == false).ToList().ForEach(c => c.StartTid = c.StartCic = null);
        }
        

        private List<GMDDF> InitGMDDFList(List<TDMIU> tdmiuList)
        {
            var e32List = service.GetE32List();

            List<GMDDF> ddfList = new List<GMDDF>();
            int idx = 1;
            foreach (var item in e32List)
            {
                var subList = tdmiuList.Where(c => c.BoardType == "E32" && c.BoradNum == item.BoardNum);
                if (subList != null && subList.Count() > 0)
                {
                    //查找全局tid
                    int gtid = subList.Min(c => c.StartTid);

                    //对于每个e32的item，有32个端口
                    for (int i = 0; i < 32; i++)
                    {
                        GMDDF obj = new GMDDF();
                        obj.Id = idx++;
                        obj.Frame = item.Frame;
                        obj.BoardNum = item.BoardNum;
                        obj.SLotNum = item.SLotNum;
                        obj.Port = i;
                        obj.GlobalTid = gtid + i * 32;
                        ddfList.Add(obj);
                    }
                }
            }
            return ddfList;
        }

        private List<GMODF> InitGMODFList(List<TDMIU> tdmiuList)
        {
            List<GMODF> odfList = new List<GMODF>();
            int idx = 1;

            var s2lList = service.GetS2lList();

            foreach (var item in s2lList)
            {
                var subList = tdmiuList.Where(c => c.BoardType == "S2L" && c.BoradNum == item.BoardNum);
                if (subList != null && subList.Count() > 0)
                {
                    //查找全局tid
                    int gtid = subList.Min(c => c.StartTid);

                    //每个s1l&s2l的item，有2个光口
                    for (int i = 0; i < 2; i++)
                    {
                        //每个光口有64个端口
                        for (int j = 0; j < 64; j++)
                        {
                            GMODF obj = new GMODF();
                            obj.Id = idx++;
                            obj.Frame = item.Frame;
                            obj.BoardNum = item.BoardNum;
                            obj.SLotNum = item.SLotNum;
                            obj.LightNum = i;
                            obj.SLotNumStr = string.Format("{0},{1}", obj.SLotNum, obj.LightNum);
                            obj.IsPrimary = item.IsPrimary;
                            obj.Port = j;
                            obj.GlobalTid = gtid + i * 64 * 32 + j * 32;
                            odfList.Add(obj);
                        }
                    }
                }

            }

            return odfList;
        }

        private List<GSDDF> InitGSDDFList()
        {
            //一一对应
            var wepiList = service.GetEpiList();
            var wcsuList = service.GetCsuList();

            List<GSDDF> list = new List<GSDDF>();

            int idx = 1;
            foreach (var item in wcsuList.OrderBy(c=>c.Frame).ThenBy(c=>c.SLotNum))
            {
                //8个端口
                for (int i = 0; i < 8; i++)
                {
                    GSDDF obj = new GSDDF();
                    obj.Id = idx++;
                    obj.Frame = item.Frame;
                    obj.SLotNum = item.SLotNum;
                    obj.CSUNum = item.CSUNum;
                    obj.Port = i;
                    list.Add(obj);
                }
                
            }
            return list;
        }

        private int? Get64KSystemNum(GSDDF obj, List<GSN7LNK> n7List)
        {
            var list = n7List.Where(c => c.LinkType.ToUpper().Contains("64K") && c.LinkSetName == obj.Direction);
            if (list != null && list.Count() > 0)
            {
                var q = list.GroupBy(c=>c.CSUNum);
                var subList = q.Select(c => new { Idx = c.Min(d => d.Idx), Csu = c.Key }).OrderBy(c => c.Idx).ToArray();
                for (int i = 0; i < subList.Length; i++)
                {
                    if (subList[i].Csu == obj.CSUNum)
                        return i + 1;
                }
            }
            return null;
        }

        private int? get2MId(GSN7LNK obj)
        {
            var ary = obj.Name.Split(new char[] { '_' }, StringSplitOptions.RemoveEmptyEntries);
            if (ary != null && ary.Length > 0)
            {
                int tmp;
                if (int.TryParse(ary[ary.Length - 1], out tmp))
                    return tmp;
            }
            return null;
        }

        private int? Get2MSystemNum(GSDDF obj, List<GSN7LNK> n7List)
        {
            var list = n7List.Where(c => c.LinkType.ToUpper().Contains("2M") && c.LinkSetName == obj.Direction)
                .Select(c => new { Id = get2MId(c), N7 = c }).Where(c => c.Id != null).OrderBy(c => c.Id).ToList();


            for (int i = 0; i < list.Count; i++)
            {
                if (list[i].N7.CSUNum == obj.CSUNum && list[i].N7.E1Port == obj.Port)
                {
                    return i + 1;
                }   
            }
            return null;
        }

        private void fillN7(List<GSDDF> ddfList, List<GSN7LNK> n7List)
        {
            var grp = n7List.GroupBy(c => new { c.CSUNum, c.LinkSetName, c.LinkType, c.E1Port });

            foreach (var g in grp)
            {
                var ddfs = ddfList.Where(c => c.CSUNum == g.Key.CSUNum && c.Port == g.Key.E1Port);
                foreach (var item in ddfs)
                {
                    item.Direction = g.Key.LinkSetName;
                        
                    item.LinkType = g.Key.LinkType;
                    if (g.Key.LinkType.ToUpper().Contains("64K"))
                    {
                        item.System = Get64KSystemNum(item, n7List);
                        item.No7Link = "TS" + string.Join("、", g.OrderBy(c => c.E1Num).Select(c => c.E1Num));
                    }
                    if (g.Key.LinkType.ToUpper().Contains("2M"))
                    {
                        item.System = Get2MSystemNum(item, n7List);
                        item.No7Link = "TS1~31";
                    }
                }
            }

        }

        
        private void fillDirection(List<AIETKC> aietkcList, List<AIETG> aietgList)
        {
            var q = from aietkc in aietkcList
                    join aietg in aietgList
                    on aietkc.GroupNum equals aietg.GroupNum
                    into t
                    from a in t.DefaultIfEmpty()
                    select new
                    {
                        AIETKC = aietkc,
                        AIETG = a
                    };
            foreach (var item in q)
            {
                if (item.AIETG != null)
                {
                    item.AIETKC.GroupName = item.AIETG.GroupName;

                }
            }
        }

        /// <summary>
        /// 填写备注
        /// </summary>
        /// <param name="gsList"></param>
        /// <param name="clkList"></param>
        private void fillRemark(List<GSDDF> gsList, List<CLKSRC> clkList)
        {
            foreach (var clk in clkList)
            {
                var l = gsList.Where(c => c.Frame == clk.Frame && c.SLotNum == clk.SLotNum && c.Port == clk.Port);
                foreach (var gs in l)
                {
                    gs.Remark = "提取时钟";
                }
            }
        }


        /// <summary>
        /// 查询MTP2信息
        /// </summary>
        /// <param name="odfList"></param>
        /// <param name="aietkcList"></param>
        private void fillMTP2(List<GMDDF> ddfList,List<GMODF> odfList, List<GMN7LNK> n7List)
        {
            foreach (var n7 in n7List)
            {
                foreach (var mtp2 in service.GetMTP2LNKList(n7.MTP2LinkNo))
                {
                    if (mtp2 != null)
                    {
                        if (mtp2.BoardTypeName == "E32")
                        {
                            var ddfs = ddfList.Where(c => c.BoardNum == mtp2.BoradNum && c.Port == mtp2.E1T1 );
                            foreach (var ddf in ddfs)
                            {
                                if (string.IsNullOrEmpty(ddf.Direction))
                                {
                                    ddf.Direction = n7.Direction;
                                    ddf.System = n7.Id;
                                }
                                else
                                {
                                    //重复，写备注
                                    ddf.Remark = "64k信令";
                                }
                                ddf.SPFNum = mtp2.SPFBoardNum;
                                ddf.KNum = mtp2.KNum;
                                if (mtp2.StartNum != null && mtp2.EndNum != null)
                                    ddf.No7Link = string.Format("TS{0}-{1}", mtp2.StartNum, mtp2.EndNum);
                            }
                        }
                        if (mtp2.BoardTypeName == "S1L" || mtp2.BoardTypeName == "S2L")
                        {
                            var odfs = odfList.Where(c => c.BoardNum == mtp2.BoradNum && c.LightNum == mtp2.LightNum && c.Port == mtp2.E1T1 );
                            foreach (var odf in odfs)
                            {
                                if (string.IsNullOrEmpty(odf.Direction))
                                {
                                    odf.Direction = n7.Direction;
                                    odf.System = n7.Id;
                                }
                                else
                                {
                                    //重复，写备注
                                    odf.Remark = "64k信令";
                                }
                                odf.SPFNum = mtp2.SPFBoardNum;
                                odf.KNum = mtp2.KNum;
                                if (mtp2.StartNum != null && mtp2.EndNum != null)
                                    odf.No7Link = string.Format("TS{0}-{1}", mtp2.StartNum, mtp2.EndNum);
                            }
                        }
                    }
                }
            }
        }

    }
}
